package com.example.android.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import androidx.appcompat.app.AlertDialog;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.android.R;
import com.example.android.bean.CarOrder;
import com.example.android.utils.BaseModel;
import com.example.android.utils.GlideEngine;
import com.example.android.utils.ImageUrlUtil;
import com.example.android.utils.NetUtil;
import com.example.android.utils.api.CarOrderApi;
import com.google.android.material.snackbar.Snackbar;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReseverItemAdapter extends BaseQuickAdapter<CarOrder, BaseViewHolder> {
    public ReseverItemAdapter(List<CarOrder> data) {
        super(R.layout.resever_list_info, data);

    }


    /**
     * 在此方法中设置item数据
     */
    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull CarOrder item) {
        //名称
        TextView userRealName = helper.findView(R.id.name);
        userRealName.setText(item.getCarRelease().getUser().getRealName());
        //车牌
        TextView chepai = helper.findView(R.id.textView19);
        chepai.setText(item.getCarRelease().getCarInfo().getPlaterNumber());
        //车型
        TextView brand = helper.findView(R.id.textView1);
        brand.setText(item.getCarRelease().getCarInfo().getBrand());

        //状态显示
        TextView status = helper.findView(R.id.textView15);
        //地址
        TextView carAddress = helper.findView(R.id.carAddress);
        carAddress.setText(item.getCarRelease().getCarInfo().getAddress());

        //收车按钮按钮
        Button button = helper.findView(R.id.yuyue);

        //设置主图长按事件
        CircleImageView profileImage = helper.findView(R.id.profile_image);
        profileImage.setOnLongClickListener(view -> {
            //弹出安全码
            new AlertDialog.Builder(getContext())
                    .setTitle("您的安全码！")
                    .setMessage("安全码:" + item.getCode())
                    .setPositiveButton("确定", null).show();
            return false;
        });
        //主图
        String imagesUrl = ImageUrlUtil.getImagesUrl(item.getCarRelease().getCarInfo());
        if (imagesUrl != null) {
            GlideEngine.createGlideEngine().loadGridImage(getContext(), imagesUrl, profileImage);
        }
        //使用时间
        TextView userDateTime = helper.findView(R.id.userDateTime);
        userDateTime.setText(item.getUseTime());

        //开始时间
        TextView start = helper.findView(R.id.textView11);
        start.setText(item.getCarRelease().getStartTime());
        //结束时间
        TextView end = helper.findView(R.id.textView13);
        end.setText(item.getCarRelease().getFinishTimew());
        //判断状态数据状态
        switch (item.getStatus()) {
            case 1:
                setText(status, "等待取车", true);
                break;
            case 2:
                setText(status, "用车中", false);
                break;
            case 3:
                setText(status, "已还车", false);
                break;
            case 4:
                setText(status, "已取消", false);
                break;
        }
        //频道按钮状态数据
        switch (item.getStatus()) {
            case 1:
                setText(button, "取消", false);
                //在取消按钮中进行绑定事件
                button.setOnClickListener(view -> NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(CarOrderApi.class).cancel(item.getId()).enqueue(new Callback<BaseModel<String>>() {
                    @Override
                    public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                        if (response.body() != null && response.body().getCode() == 200) {
                            Snackbar.make(view, "取消成功：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                        } else {
                            Snackbar.make(view, "不能取消：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                        Snackbar.make(view, "取消失败：" + t.getMessage(), Snackbar.LENGTH_LONG).show();

                    }
                }));
                break;
            case 2:
                setText(button, "用车中", true);
                break;
            case 3:
                setText(button, "已还车", true);
                break;
            case 4:
                setText(button, "已取消", true);
                break;
        }

    }

    private void setText(Button button, String text, Boolean isBlackColor) {
        if (isBlackColor) {
            button.setTextColor(Color.BLACK);
            button.getBackground().setAlpha(255);
        }
        button.setText(text);
    }

    private void setText(TextView textView, String text, Boolean isBlackColor) {
        if (isBlackColor) {
            textView.setVisibility(View.VISIBLE);
        }
        textView.setText(text);
    }
}
